home *** CD-ROM | disk | FTP | other *** search
/ Language/OS - Multiplatform Resource Library / LANGUAGE OS.iso / prolog / ai.prl / mike1.exe / EXTRAS.PL < prev    next >
Encoding:
Text File  |  1990-09-14  |  3.6 KB  |  111 lines

  1. /* extras.pl:
  2.    Extensions to MIKE.  See MIKE.INI for an explanation of the
  3.    ?- allow_prolog... declarations in this file.
  4.  
  5. The following facilities are defined in this file:
  6. 1. Simple multiple-choice menus
  7. 2. A 'forall' facility
  8. */
  9.  
  10. /* special menu handling code */
  11. /* next line makes 'ask_menu' legal on right hand side of MIKE rules */
  12.  
  13. ?- allow_prolog_rhs(ask_menu(_,_,_)).
  14.  
  15. /*
  16. 'ask_menu' facility (right hand side of MIKE rules only)
  17. ---------------------------------------------------------
  18. ask_menu(Object, Relation, List)  displays a numbered menu of
  19. all the items in List and prompts the user to type in number(s).
  20.  
  21.    For example:
  22.      rule a forward
  23.       if
  24.         diagnosing &
  25.         [current_patient, P]
  26.       then
  27.         ask_menu(P,exhibits_symptom,[sneezing,coughing,headache,spots]).
  28.  
  29.    The end-user types in the numbers (1,2,3,4, or some combination, in
  30.    this case) and the corresponding list elements are then stored
  31.    in working memory as a triple: [Object, Relation, Choice]
  32.    for each numbered Choice which was presented in the List.
  33.    e.g.         [john, exhibits_symptom, sneezing]
  34.    An example of its use may be found in the file FLU2.KB
  35.  
  36. */
  37.  
  38. ask_menu(_,_,[]):- !,
  39.     'pd624 write'(['Error: ask menu must be called with a list of menu',nl,
  40.                            'elements, not with an empty list',nl]).
  41. ask_menu(Object,Relation,List):-
  42.     'pd624 write'(['**********************************************',nl]),
  43.     draw_and_read_menu(1,List,Menu),
  44.     'pd624 write'(['Choose the items from the menu by typing the',nl,
  45.            'corresponding number(s). Separate numbers with commas e.g.',nl,
  46.            '1,3,5.',nl,
  47.            'REMEMBER to use a FULL STOP (''.'') at the end',nl,'==> ']),
  48.     read(Selections),
  49.     add_selections_to_wm(Object,Menu,Selections,Relation),
  50.     nl.
  51.  
  52. draw_and_read_menu(_,[],[]).
  53. draw_and_read_menu(N,[H|T],[N-H|Rest]):-
  54.     'pd624 write'([t/6,N,'   -   ',H,nl]),
  55.     N1 is N + 1,
  56.     draw_and_read_menu(N1,T,Rest).
  57.  
  58. add_selections_to_wm(Thing,Menu,(A,B),Relation):-
  59.     'pd624 member'(A-Item,Menu),
  60.     add [Thing,Relation,Item],
  61.     add_selections_to_wm(Thing,Menu,B,Relation).
  62. add_selections_to_wm(Thing,Menu,(A,B),Relation):- !,
  63.     'pd624 write'(['Error : ',A,' is not a legal menu entry and will',nl,
  64.     'be ignored.',nl]),
  65.     add_selections_to_wm(Thing,Menu,B,Relation).
  66. add_selections_to_wm(Thing,Menu,Num,Relation):-
  67.    'pd624 member'(Num-Item,Menu),
  68.    add [Thing,Relation,Item].
  69. add_selections_to_wm(_,_,A,_):-
  70.     'pd624 write'(['Error : ',A,' is not a legal menu entry and will',nl,
  71.     'be ignored.',nl]).
  72.  
  73. /* FORALL */
  74.  
  75. /*
  76. 'forall' facility (for left hand side of MIKE rules only)
  77. -----------------------------------------------------------
  78. forall(Pattern1, Pattern2) tests whether all of the variables
  79. which match working memory within Pattern1 also match working memory
  80. within Pattern2.  For instance, suppose working memory contains
  81. the following patterns:
  82.    [john, likes, mary]
  83.    [john, likes, sue]
  84.    [john, likes, betty]
  85.    [bill, likes, mary]
  86.    [bill, likes, betty]
  87.    [bill, likes, sue]
  88.  
  89. The following rule succeeds with the above working memory:
  90.  
  91.   rule b forward
  92.    if
  93.     forall([john, likes, X], [bill, likes, X])
  94.    then
  95.     announce ['bill likes all the people that john likes'].
  96.  
  97. If we typed in ?- remove [bill, likes, sue].
  98. Then the above rule b would fail
  99. */
  100.  
  101. ?- allow_prolog_lhs(forall(_,_)).
  102.  
  103. forall(Pattern1, Pattern2) :-
  104.   setof(in_wm(Pattern2), in_wm(Pattern1), Cases),
  105.   forall_tryall(Cases).
  106.  
  107. forall_tryall([Case|Cases]) :-
  108.   call(Case),
  109.   forall_tryall(Cases).
  110.  
  111. forall_tryall([]).